//
//  GHSOD_PLViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/16.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSOD_PLViewController.h"
#import "GHSOD_SectionHeaderView.h"
#import "GHSOD_GoodsTableViewCell.h"
#import "GHSOD_PL_UserinfoTableViewCell.h"
#import "GHSOD_PL_OrderinfoTableViewCell.h"
#import "GHSOD_PL_CommentInfoTableViewCell.h"

@interface GHSOD_PLViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray<SPSection *> *sections;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) GHSOD_PL_CommentInfoTableViewCell *commentInfoCell;

@end

@implementation GHSOD_PLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"评论详情";
    [self initDataSource];
    [self js_commentCellH];
    [self setupTableView];
}


- (void)initDataSource
{
    NSArray *items = @[@"商品信息",@"买家信息",@"评价信息",@"订单信息"];
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SPSection *sec = [SPSection new];
        sec.title = obj;
        [self.sections addObject:sec];
    }];
}


- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView =tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];

    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_SectionHeaderView class]) bundle:nil]  forHeaderFooterViewReuseIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_GoodsTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_PL_UserinfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_PL_UserinfoTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_PL_OrderinfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_PL_OrderinfoTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_PL_CommentInfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_PL_CommentInfoTableViewCell class])];
    
}


#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    switch (indexPath.section) {
        case 0:
            height = 160;
            break;
        case 1:
            height = 100;
            break;
        case 2:
            height = self.commentInfoCell.cellH;
            break;
        case 3:
            height = 125;
            break;
    }
    return height;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPSection *sec = _sections[section];
    GHSOD_SectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    headerV.title.text = sec.title;
    headerV.arrow.hidden = YES;
    return headerV;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPSection *sec = self.sections[indexPath.section];
    UITableViewCell *cell;
    switch (indexPath.section) {
        case 0:{
            GHSOD_GoodsTableViewCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
            cell = goodsCell;
            break;
        }
        case 1:{
            GHSOD_PL_UserinfoTableViewCell *userInofCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_PL_UserinfoTableViewCell class])];
            cell = userInofCell;
            break;
        }
        case 2:{
            cell = self.commentInfoCell;
            break;
        }
        case 3:{
            GHSOD_PL_OrderinfoTableViewCell *orderInfoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_PL_OrderinfoTableViewCell class])];
            cell = orderInfoCell;
            break;
        }
    }
    return cell;
}



- (void)js_commentCellH
{
    [self.commentInfoCell cellH];
    [self.tableView reloadData];
}


#pragma mark--setter&&getter

- (NSMutableArray<SPSection *> *)sections
{
    if(!_sections){
        _sections = [NSMutableArray array];
    }
    return _sections;
}


- (GHSOD_PL_CommentInfoTableViewCell *)commentInfoCell
{
    if(!_commentInfoCell){
       _commentInfoCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_PL_CommentInfoTableViewCell class])];
    }
    return _commentInfoCell;
}

@end
